package com.iwhalecloud.bss.kite.cucc.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.iwhalecloud.bss.kite.client.enums.TriggerStrategy;
import com.iwhalecloud.bss.kite.service.attr.AttrData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname CommonUtil
 * @Description 常用方法工具类
 * @Author zhang.song
 * @Date 2020-09-22 10:06
 */
public class CommonUtil {

    /**
     * 判断字符是否是数字
     * @author zhang.song
     * @date 2020-09-22 10:06
     * @param num
     * @return boolean
     */
    public static boolean isInteger(String num) {
        if (num == null || "".equals(num)) {
            return false;
        }
        String regex = "^\\d+$";
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(num);
        return isNum.matches();
    }

    public static List convertToList(Object o) {
        if(o instanceof List){
            return (List)o;
        } else {
            List temp = new ArrayList();
            if (o instanceof Map) {
                temp.add(o);
            }
            return temp;
        }
    }

    public static List getList(Map<String, Object> map, String key) {
        if(map != null) {
            Object o = map.get(key);
            if(o instanceof List){
                return (List)o;
            } else {
                if (o instanceof Map) {
                    List temp = new ArrayList();
                    temp.add(o);
                    return temp;
                }
            }
        }
        return null;
    }

    /**
     * 接口返回报文是里的节点json字符串，转Map
     * @author zhang.song
     * @date 2021-02-26 15:31
     * @param map
     * @param key
     * @return java.util.Map
     */
    public static Map getMap(Map<String, Object> map, String key) {
        if(map != null) {
            Object o = map.get(key);
            if(o instanceof Map){
                return (Map) o;
            } else if(o instanceof List){
                return ((List<Map>) o).get(0);
            } else {
                if (o instanceof String) {
                    try {
                        return JSON.parseObject((String) o, Map.class);
                    } catch (JSONException e) {
                        return null;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取属性处理器AttrData入参json串
     * @param triggerStrategy
     * @return
     */
    public static String getAttrDataJson(TriggerStrategy triggerStrategy) {
        // 设置入口参数，有该参数则不执行属性规则
        Map<String, Object> extAttributes = new HashMap<>();
        AttrData attrData = new AttrData();
        attrData.setExtAttributes(extAttributes);
        attrData.setTriggerStrategy(triggerStrategy);
        return JSON.toJSONString(attrData);
    }

    /**
     * 获取属性处理器AttrData入参json串
     * @param triggerStrategy
     * @return
     */
    public static String getAttrDataJsonOfLoadTmpl(TriggerStrategy triggerStrategy) {
        // 设置入口参数，有该参数则不执行属性规则
        Map<String, Object> extAttributes = new HashMap<>();
        extAttributes.put("entry", "sceneTempl");
        AttrData attrData = new AttrData();
        attrData.setExtAttributes(extAttributes);
        attrData.setTriggerStrategy(triggerStrategy);
        return JSON.toJSONString(attrData);
    }

    /**
     * 拼接合同、用户、账户等实例id，若实例id以前缀字符串开头，则不再拼接前缀字符
     * @author zhang.song
     * @date 2021-07-05 10:23
     * @param prefix
     * @param instanceId
     * @return java.lang.String
     */
    public static String getInstanceId(String prefix, String instanceId) {
        if(instanceId != null && instanceId.startsWith(prefix)) {
            return instanceId;
        }
        return prefix + instanceId;
    }

    /**
     * 字符串按逗号分割后，是否包含目标字符串
     * @author zhang.song
     * @date 2021-11-15 17:00
     * @param str
     * @param key
     * @return boolean
     */
    public static boolean splitContains(String str, String key) {
        if(str == null || key == null) {
            return false;
        }

        String[] arr = str.split(",");
        for (String s : arr) {
            if(key.equals(s)) {
                return true;
            }
        }
        return false;
    }
}
